let axios = require('axios');
let { randomString } = require("../utils/string")
const access_token = 'b08ba21f610f259121f0b9e1acee7c3e'
module.exports = {
    async Pupload(req, resp) {
        if (req.body.content.length * 0.75 > 2000000) { resp.send({ code: -2, "message": "文件过大" }); return; }
        if (!req.query.fileName) { resp.send({ code: -2 }); return; }

        let FormData = require('form-data');
        var data = JSON.stringify({
            "Pic-Size": "0*0",
            "Pic-Encoding": "base64",
            "Pic-Path": "/nowater/webim/big/",
            "Pic-Data":""
        })


        let d = new Date()
        let url = 'https://gitee.com/api/v5/repos/bendantada/img/contents/' + `${d.getFullYear()}-${d.getMonth()}-${d.getDate()}/` + randomString(6) + encodeURIComponent(req.query.fileName)

        let config = {
            method: 'post',
            url: 'https://upload.58cdn.com.cn/json',
            headers: {
                'Host': 'gitee.com',
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.76',
                'Content-Type': 'application/json',
                'Accept': '*/*',
                'Connection': 'keep-alive'
            },
            data: data
        };

        let re = await axios(config)
        await database.imgs.create({ url: re.data.content.download_url, name: fileName, fromid: req.query.userid, msgid: req.query.msgid, msgtype: req.query.msgtype })
        resp.send({ msg: re.data })
    }
}